<template>
  <VPage>
    <VTable ref="tableRef" v-bind="tableOptins">
      <template #form="{ form }">
        <XForm
          :fieldList="fieldList"
          :formData="form"
        >
        </XForm>
      </template>
      <template #toolbar_btns>
        <VActionBar :actions="actions" />
      </template>
      <template #operation="{ row }">
        <el-button
          v-hasPermi="[sysCostConfigAuthList.initial_cost.edit]"
          link
          type="primary"
          @click="handleAdd(row)"
        >
          编辑
        </el-button>
        <el-button link type="primary" @click="handleLog(row)">
          日志
        </el-button>
      </template>
    </VTable>
    <Add
      :id="addId"
      v-model="addVisible"
      @close="addId = null"
      @refresh="refresh"
    />
    <SpecialLog
      dialog
      v-model="logVisible"
      :autoLoad="false"
      tag="SYS_COST_CONFIG"
      :business_id="logId"
      @close="logId = ''"
    />
  </VPage>
</template>
<script setup lang="jsx">
import { ElMessage } from "element-plus";
import XForm from "@/components/XForm/index.vue";
import Add from "./add/index";
import { getTableSelect, tipMessage } from "@/hooks/tool.js";
import { sysCostConfigAuthList } from "@/Auth/sys-cost-config.js";
import SpecialLog from "@/components/SpecialLog/index.vue";
import { tcFeeConfigList, tcFeeConfigStatus } from "./api.js";
const { proxy } = getCurrentInstance();
const tableRef = ref();
const tableOptins = reactive({
  formConfig: {
    data: {},
  },
  tooltipConfig: {
    showAll: false,
  },
  isToggle: true,
  columns: [
    { fixed: "left", width: 50, type: "checkbox" },
    { fixed: "left", width: 50, title: "编号", type: "seq" },
    { title: "费用标识", width: 110, field: "cost_ident" },
    { title: "费用名称", minWidth: 260, field: "cost_label" },
    {
      title: "状态",
      width: 100,
      field: "status",
      slots: {
        default: ({ row }) => {
          return <VstatusRender code={row} />;
        },
      },
    },
    {
      title: "操作",
      width: 120,
      fixed: "right",
      slots: { default: "operation" },
    },
  ],
  proxyConfig: {
    autoLoad: false, // 是否自动加载查询数据
    props: {
      result: "data",
      total: "total",
    },
    ajax: {
      query: ({ page, form }) => {
        return tcFeeConfigList({ ...form, ...page }).then((res) => {
          return {
            data: res.data,
            total: res.total,
          };
        });
      },
    },
  },
});
const addId = ref(null);
const addVisible = ref(false);
const handleAdd = (item) => {
  addVisible.value = true;
  addId.value = item?.id || null;
};
const refresh = () => {
  tableRef?.value.$table.commitProxy("query");
};
const handleStatus = (status) => {
  const rows = getTableSelect(tableRef.value);
  if (!rows.length) return ElMessage.warning("请至少选择一条数据!");
  handleStatusReq(rows, status);
};
const handleStatusReq = (id, flag) => {
  proxy?.confirm({
    title: "提示",
    content: (
      <p>
        确认{flag ? "启用" : "禁用"}选中的
        <span class="text-[#3366F1] px-[4px]">{id.length || 1}</span>
        条数据？
      </p>
    ),
    beforeClose: async (action, instance, done) => {
      if (action === "confirm") {
        try {
          instance.confirmButtonLoading = true;
          instance.confirmButtonText = "处理中..";
          const { data, code, message } = await tcFeeConfigStatus({
            id,
            status: flag ? 1 : 0,
          });
          if (code !== 200) return ElMessage.warning(message);
          tipMessage(data);
        } catch (e) {
          console.error(e);
        } finally {
          instance.confirmButtonLoading = false;
          done();
          refresh();
        }
      } else {
        done();
      }
    },
  });
};
const logId = ref("");
const logVisible = ref(false);
const handleLog = (item) => {
  logId.value = item.id;
  logVisible.value = true;
};
// const handleStatus = () => {};
const actions = ref([
  {
    label: "新增",
    icon: "add2",
    fn: () => {
      addVisible.value = true;
    },
    type: "primary",
    permission: [sysCostConfigAuthList.initial_cost.add],
  },
  {
    label: "启用",
    icon: "state_finish_line",
    fn: () => handleStatus(true),
    permission: [sysCostConfigAuthList.initial_cost.enabled],
  },
  {
    label: "禁用",
    icon: "state_disabled",
    fn: () => handleStatus(false),
    permission: [sysCostConfigAuthList.initial_cost.disabled],
  },
]);
const publicStyle = { style: { width: "240px" } };
const fieldList = [
  {
    field: "cost_label",
    placeholder: "费用名称",
    type: "input",
    ...publicStyle,
  },
];
</script>
